package manager;

import java.util.HashMap;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import structures.clipboard.IClipboardObject;

import constants.Globals;
import domain.UserDAO;
import exception.IBToolsException;

public class SessionManager {

	private static final String CLIENT_IP = "clientip";
	private static final String HEIGHT = "height";
	private static final String WIDTH = "width";
	private static final String LOGGED_IN = "loggedIn";
	private static final String LOGIN_USER_CODE = "loginUser";
	private static final String GLOBAL_USER_CODE = "globalUser";
	private static final String SESSION_COOKIE_PLM = "plmCookie";
	private static final String SESSION_COOKIE_KYS = "kysCookie";
	private static final String ENCRYPTED_FAUD_PASS = "encryptedFaudPass";
	private static final String NT_PASS = "ntPass";
	private static final String KEEPING_LOG = "logging";
	private static final String THEME = "theme";
	private static final String TRX_LEVEL = "trxlevel";
	private static final String CLIPBOARD = "clipBoard";
	
	public static void reloadSession(){
		HttpServletRequest httpServletRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();  
		String loginUser = httpServletRequest.getRemoteUser(); 
		if(loginUser.indexOf("FINANS") > -1)
			loginUser = loginUser.substring(7);
		String ip = httpServletRequest.getHeader("FBCIP");
		if(ip == null)
			ip = httpServletRequest.getRemoteAddr();
		
		setClientIp(ip);
		setLoginUserCode(loginUser);
		setGlobalUserCode(loginUser);
		setNtPass("Diren24680");
//		setTheme("sincap");
//		if(getNoReloadParameter(THEME) == null){
			reloadTheme();
//		}

		System.out.print("Session Refresh --- ");
		System.out.print("User: " + SessionManager.getLoginUserCode());
		System.out.println("");
		
	}
	
	public static void reloadTheme(){
//		if(getLoginUserCode().equalsIgnoreCase("T30412"))
//			setTheme("park");
//		else if(getLoginUserCode().equalsIgnoreCase("T20651"))
//			setTheme("kitty");
//		else if(getLoginUserCode().equalsIgnoreCase("T32556"))
//			setTheme("park");
//		else
		try {
			UserDAO user = UserManager.getUserInfoFromDB(SessionManager.getLoginUserCode());
			String theme = null;
			if(user != null){
				theme = user.getCurrentTheme();
			}
			if( theme == null || theme.equalsIgnoreCase(Globals.EMPTY_STRING)){
				setTheme("start");
			}
			else{
				setTheme(theme);
			}
		} catch (IBToolsException e) {
			setTheme("start");
			e.printStackTrace();
		}
	}

	private static Object getParameter(String sessionParameter){
		Object parameter = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(sessionParameter);
		if(parameter == null){
			reloadSession();
			parameter = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(sessionParameter);
		}
		return parameter;
	}
	
	private static void setParameter(String parameterName, Object sessionParameter){
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(parameterName, sessionParameter);
	}
	
	public static String getClientIp(){
		Object ip = getParameter(CLIENT_IP);
		if(ip == null)
			return null;
		return ip.toString();
	}
	public static void setClientIp(String clientIp){
		setParameter(CLIENT_IP, clientIp);
	}

	public static String getLoginRealUserCode(){
		Object user = getParameter(LOGIN_USER_CODE);
		if(user == null)
			return null;
		return user.toString();
	}
	public static String getLoginUserCode(){
		Object user = getParameter(GLOBAL_USER_CODE);
		if(user == null || user.equals(Globals.EMPTY_STRING)){
			user = getParameter(LOGIN_USER_CODE);
			if(user == null)
				return null;
		}
		return user.toString();
	}
	public static void setLoginUserCode(String loginUserCode){
		setParameter(LOGIN_USER_CODE, loginUserCode);
	}
	public static void setGlobalUserCode(String loginUserCode){
		setParameter(GLOBAL_USER_CODE, loginUserCode);
	}
	
	public static String isLoggedIn(){
		Object loggedIn = getParameter(LOGGED_IN);
		if(loggedIn == null)
			return "false";
		return "true";
	}
	public static void setLoggedIn(String loggedIn){
		setParameter(LOGGED_IN, loggedIn);
	}

	public static String getHeight(){
		Object height = getParameter(HEIGHT);
		if(height == null)
			return null;
		return height.toString();
	}
	public static void setHeight(String height){
		setParameter(HEIGHT, height);
	}

	public static String getWidth(){
		Object width = getParameter(WIDTH);
		if(width == null)
			return null;
		return width.toString();
	}
	public static void setWidth(String width){
		setParameter(WIDTH, width);
	}
	public static String getSessionCookiePLM(){
		Object plmCookie = getParameter(SESSION_COOKIE_PLM);
		if(plmCookie == null)
			return null;
		return plmCookie.toString();
	}
	public static void setSessionCookiePLM(String plmCookie){
		setParameter(SESSION_COOKIE_PLM, plmCookie);
	}
	public static String getSessionCookieKYS(){
		Object kysCookie = getParameter(SESSION_COOKIE_KYS);
		if(kysCookie == null)
			return null;
		return kysCookie.toString();
	}
	public static void setSessionCookieKYS(String kysCookie){
		setParameter(SESSION_COOKIE_KYS, kysCookie);
	}
	public static String getEncryptedFaudPass(){
		Object pass = getParameter(ENCRYPTED_FAUD_PASS);
		if(pass == null)
			return null;
		return pass.toString();
	}
	public static void setEncryptedFaudPass(String pass){
		setParameter(ENCRYPTED_FAUD_PASS, pass);
	}
	public static String getNtPass(){
		Object pass = getParameter(NT_PASS);
		if(pass == null)
			return null;
		return pass.toString();
	}
	public static void setNtPass(String pass){
		setParameter(NT_PASS, pass);
	}

	public static boolean isLogging(){
//		Object logging = getParameter(KEEPING_LOG);
//		if(logging == null)
//			return true;
//		return logging.equals("T");
		return true;
	}
	public static void setLogging(boolean logging){
		setParameter(KEEPING_LOG, logging ? "T" : "F");
	}

	public static boolean isAdminUser(){
		boolean isAdminUser = getLoginUserCode().equals("T32556") ;
		return isAdminUser;
	}

	public static String getTheme(){
		Object theme = getParameter(THEME);
		if(theme == null)
			return null;
		return theme.toString();
	}
	public static void setTheme(String theme){
		setParameter(THEME, theme);
	}
	public static Integer getTrxLevel(){
		Object trxLevel = getParameter(TRX_LEVEL);
		if(trxLevel == null)
			return null;
		return Integer.parseInt(trxLevel.toString());
	}
	public static void setTrxLevel(Integer trxLevel){
		setParameter(TRX_LEVEL, trxLevel);
	}
	
	public static String getClipBoard(){
		Object clipBoard = getParameter(CLIPBOARD);
		if(clipBoard == null)
			return null;
		return clipBoard.toString();
	}
	public static void setClipBoard(String clipBoard){
		setParameter(CLIPBOARD, clipBoard);
	}

	@SuppressWarnings("unchecked")
	public static HashMap<String, IClipboardObject> getClipBoardObjectMap(String clipboardMapType){
		Object mapObject = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(clipboardMapType);
		HashMap<String, IClipboardObject> map = null;
		if(mapObject != null)
			map = (HashMap<String, IClipboardObject>) mapObject;
		return map;
	}
	public static void setClipBoardObjectMap(String clipboardMapType, HashMap<String, IClipboardObject> map){
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(clipboardMapType, map);
	}
	
	public static IClipboardObject getClipBoardObject(String clipboardMapType, String id){
		return getClipBoardObjectMap(clipboardMapType).get(id);
	}
	
	public static void setClipBoardObject(String clipboardMapType, IClipboardObject clipboardObject){
		HashMap<String, IClipboardObject> map = getClipBoardObjectMap(clipboardMapType);
		if(map == null){
			map = new HashMap<String, IClipboardObject>();
		}
		map.put(clipboardObject.getId(), clipboardObject);
		setClipBoardObjectMap(clipboardMapType, map);
	}
}
